﻿@using Smartstore.Engine.Modularity

@inject ModuleManager ModuleManager

@{
    Layout = "_AdminLayout";

    var providerMetadata = ViewBag.Provider as ProviderMetadata;

    if (providerMetadata == null)
    {
        var message = $"The '_ConfigureProvider' layout requires an instance of '{nameof(ProviderMetadata)}' which you can pass via 'ViewBag.Provider'.";
        throw new InvalidOperationException(message);
    }

    var providerModel = ModuleManager.ToProviderModel(providerMetadata);
    var returnUrl = Context.GetRouteValueAs("returnUrl", Url.BackToProviderList(providerMetadata));

    ViewBag.Title = T("Admin.Common.Configure").Value + " - " + providerModel.FriendlyName;
}

<div class="section-header">
    <div class="title" title="@ViewBag.Title (@providerModel.SystemName)">
        @if (returnUrl.HasValue())
        {
            <a sm-backto href='@returnUrl'></a>
        }
        <img src="@Url.Content(providerModel.IconUrl)" alt="@providerModel.FriendlyName" />
        <span>@ViewBag.Title</span>
    </div>
    @if (providerModel.ModuleDescriptor != null)
    {
        <div class="options">
            <zone name="admin_button_toolbar_before" />
            <a class="update-string-resources btn btn-secondary" href='@Url.Action("UpdateStringResources", "Module")' data-system-name="@providerModel.ModuleDescriptor.SystemName">
                <i class="fa fa-sync-alt"></i>
                <span>@T("Admin.Configuration.Plugins.Resources.Update")</span>
            </a>
            <zone name="admin_button_toolbar_after" />
        </div>
    }
</div>

<div class="plugin-config-container" attr-class='(ViewBag.IsGrid == true, "spa-stretch")'>
    @RenderBody()
</div>

<script sm-target-zone="scripts" data-origin="ConfigureProvider">
    $(function () {
        $('.section-header .options').on('click', '[type=submit]', function (e) {
            // On SubmitButtonClick, post the form programmatically, as the button is not a child of the form...
            var btn = $(e.currentTarget);
            let form = !!(btn.data("target"))
                ? $(btn.data("target")).first()
                : $('.plugin-config-container form').first();

            if (form.length) {
                // ...but first add a hidden input to the form with button's name and value to mimic button click WITHIN the form.
                form.prepend($('<input type="hidden" name="' + btn.attr('name') + '" value="' + btn.attr('value') + '" />'));
                form.submit();
            }
        });

        $(".update-string-resources").on("click", function (e) {
            e.preventDefault();

            var $el = $(this);

            $({}).ajax({
                type: 'POST',
                url: $el.attr("href"),
                data: {
                    "systemName": $el.attr("data-system-name")
                },
                success: function (data) {
                    displayNotification(data.Message, data.Success ? "success" : "error");
                }
            });

            return false;
        })
    });
</script>
